package com.lzb.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzb.reggie.common.R;
import com.lzb.reggie.dto.OrdersDto;
import com.lzb.reggie.entity.Orders;
import com.lzb.reggie.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    @CacheEvict(value = "orderCache", allEntries = true)
    public R<String> submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 客户端订单分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    @Cacheable(value = "orderCache", key = "#p0 + '_' + #p1")
    public R<Page<OrdersDto>> userPage(int page, int pageSize) {

        Page<OrdersDto> ordersDtoPage = ordersService.userPage(page, pageSize);

        return R.success(ordersDtoPage);
    }

    /**
     * 后台订单分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    @Cacheable(value = "orderCache", key = "#p0 + '_' + #p1 + '_' + #p2 + '_' + #p3 + '_' + #p4")
    public R<Page<OrdersDto>> page(int page, int pageSize, Long number, @DateTimeFormat String beginTime, @DateTimeFormat String endTime) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();

        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(number != null, Orders::getId, number);
        wrapper.between(beginTime != null && endTime != null, Orders::getOrderTime, beginTime, endTime);
        wrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage, wrapper);

        List<OrdersDto> dtoList = ordersPage.getRecords().stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);
            ordersDto.setUserName(item.getConsignee());
            return ordersDto;
        }).collect(Collectors.toList());

        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");
        ordersDtoPage.setRecords(dtoList);
        return R.success(ordersDtoPage);
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    @CacheEvict(value = "orderCache", allEntries = true)
    public R<String> updateStatus(@RequestBody Orders orders) {
        ordersService.updateById(orders);
        return R.success("修改成功");
    }

    /**
     * 再来一单
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders) {
        ordersService.again(orders);
        return R.success("true");
    }
}
